package com.bruce.apkinstaller.install;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/1/17.
 */

public class ArgsInstallerModel {
    private static final String TAG = ArgsInstallerModel.class.getSimpleName();

    /**
     * 使用命令行安装应用
     *
     * @param apkAbsolutePath
     * @return
     */
    public static void install(String apkAbsolutePath, ProductArgsInstallImpl.ArgsPackageInstall argsInstallListener) {
        String[] args = {"pm", "install", "-r", apkAbsolutePath};
        // String[] args = { "adb", "install", "-r", apkAbsolutePath };
        String result = "";
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            int installed = 0;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
                installed += read;
                argsInstallListener.installProgress(apkAbsolutePath, installed);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
            argsInstallListener.packageInstalled(apkAbsolutePath);
        } catch (Exception e) {
            argsInstallListener.installFailure(apkAbsolutePath, e);
            e.printStackTrace();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        if (result != null && (result.endsWith("Success") || result.endsWith("Success\n"))) {
        } else {
            argsInstallListener.installFailure(apkAbsolutePath, result);
        }
    }

    /**
     * 使用命令行卸载应用
     *
     * @param packageName
     */
    public void uninstall(String packageName, ProductArgsUninstallImpl.ArgsPackageDelete argsPackageDelete) {
        String[] args = {"pm", "uninstall", packageName};
        // String[] args = { "adb", "uninstall", packageName };
        String result = null;
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
    }
}
